var enterprise = $('[name="enterprise"]');
var account = $('[name="account"]');
var pwd = $('[name="pwd"]');
enterprise.val("");
account.val("");
pwd.val("");
var isent = false, isacc = false, ispwd = false;
if (getCookie("learn")) {
    window.location.href = "/";
}
account.blur(function () {
    var text = $(this).val();
    var pattern = /^\S{6,20}$/;
    if (!pattern.test(text)) {
        $(this).next().text("6-20位非空字符！").show();
        isacc = false;
    } else {
        $(this).next().hide();
        isacc = true;
    }
});
pwd.blur(function () {
    var text = $(this).val();
    var pattern = /^\S{6,20}$/;
    if (!pattern.test(text)) {
        $(this).next().text("6-20位非空字符").show();
        ispwd = false;
    } else {
        $(this).next().hide();
        ispwd = true;
    }
});
$('#but').click(function () {
    var a = $('#form').serializeArray();
    if ($('input[type="checkbox"]').is(":checked")) {
        a.push({'name': 'selected', 'value': $('#learn').val()});
    }
    $.post("/api/user/login", a, function (json) {
       var a =json;
        a = a.replace(/[^0-9]/ig,""); 
       if(a=="102"||a=="101"){
          $('#alert').show();
       }else{
        var data = eval(json);
        Cookie.Set("account", data[0].account);
        Cookie.Set("level", data[0].parentmyid);
        Cookie.Set("myid", data[0].myid);
        document.location = '/';
    }
    });
})

var tx = "";
$('input[name=account]').blur(function () {
    $('#enterprise').empty();
    var acc = $('input[name=account]').val();
    new Ajax("/api/login/select").keyvalue("account", acc).getJson(function (json) {
        $.each(json, function (k, v) {
            $('#enterprise').append("<option value='" + v["enterprise"] + "'>" + v["enterprise"] + "</option>");
        });
    });
})
//点击回车键登陆
$('[name="pwd"]').keydown(function (e) {
    if (e.keyCode == 13) {
        $(this).blur();
        $('#login_div button').click();
    }
});
//弹框方法
function tooltip(text) {
    $('#alert').show().find('p').text(text);
    $('#alert button').click(function () {
        $('#alert').hide();
    });
}
///获取cookie值
function getCookie(NameOfCookie) {
    if (document.cookie.length > 0) {
        begin = document.cookie.indexOf(NameOfCookie + "=");
        if (begin != -1) {
            begin += NameOfCookie.length + 1;
            end = document.cookie.indexOf(";", begin);
            if (end == -1)
                end = document.cookie.length;
            return unescape(document.cookie.substring(begin, end));
        }
    }
    return null;
}
$('#ale').click(function(){
    $('#alert').hide();
})